package com.googlecode.gwt.commons.logging.jdk14log;

/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.logging.impl.AbstractLogger;
import org.apache.commons.logging.impl.MetaTagsUtility;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
 * <p>
 * Implementation of the <code>org.apache.commons.logging.Log</code> interface
 * that wraps the standard JDK logging mechanisms that were introduced in the
 * Merlin release (JDK 1.4).
 * </p>
 * 
 * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
 * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
 * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
 * @version $Revision: 1.13 $ $Date: 2004/06/06 21:10:21 $
 */

public class Jdk14Logger extends AbstractLogger {

	private static final long serialVersionUID = -1757574918060337429L;
	
	public Jdk14Logger() {
	}

	/**
	 * Construct a named instance of this Logger.
	 * 
	 * @param name
	 *            Name of the logger to be constructed
	 */
	public Jdk14Logger(String name) {
		init(name);
	}

	public void init(String name) {
		this.loggerName = name;
		logger = getLogger();
	}
	
	/**
	 * The underlying Logger implementation we are using.
	 */
	private transient Logger logger = null;
	
	protected boolean log(Level level, String msg, Throwable ex) {
		
		if(sendLog){
			logger.setLevel(Level.ALL);
		}
		else{
			logger.setLevel(orig);
		}
		
		boolean isLoggable = logger.isLoggable(level);
		if(isLoggable) {
			StackTraceElement locations = getStackTraceElement();
			//String cname = "unknown";
			String method = "unknown";
			if (locations != null) {
				//cname = locations.getClassName();
				method = locations.getMethodName();
			}
			if (ex == null) {
				logger.logp(level, getName(), method, msg);
				gwtShellLog(level.getName(), msg, null);
			} else {
				logger.logp(level, getName(), method, msg, ex);
				gwtShellLog(level.getName(), msg, ex);
			}
		}
		
		return isLoggable;
	}

	/**
	 * Log a message with debug log level.
	 */
	public void debug(Object message) {
		log(Level.FINE, String.valueOf(message), null);
	}

	/**
	 * Log a message and exception with debug log level.
	 */
	public void debug(Object message, Throwable exception) {
		log(Level.FINE, String.valueOf(message), exception);
	}

	/**
	 * Log a message with error log level.
	 */
	public void error(Object message) {
		log(Level.SEVERE, String.valueOf(message), null);
	}

	/**
	 * Log a message and exception with error log level.
	 */
	public void error(Object message, Throwable exception) {
		log(Level.SEVERE, String.valueOf(message), exception);
	}

	/**
	 * Log a message with fatal log level.
	 */
	public void fatal(Object message) {
		log(Level.SEVERE, String.valueOf(message), null);
	}

	/**
	 * Log a message and exception with fatal log level.
	 */
	public void fatal(Object message, Throwable exception) {
		log(Level.SEVERE, String.valueOf(message), exception);
	}

	private static boolean sendLog = false;
	private static transient Level orig = null;
	
	/**
	 * Return the native Logger instance we are using.
	 */
	public Logger getLogger() {
		if (logger == null) {
			logger = Logger.getLogger(getName());

			final Element[] metas = MetaTagsUtility.getMetas("gwtx:property");
			for (int i = 0; i < metas.length; i++) {
				final Element meta = metas[i];
				final String content = DOM.getElementProperty(meta, "content");
				final int eq = content.indexOf("=");
				if (eq != -1) {
					final String cname = content.substring(0, eq);
					if ((".level").equals(cname)) {
						final String cvalue = content.substring(eq + 1);
						orig =Level.parse(cvalue);
						logger.setLevel(Level.parse(cvalue));
						break;
					}
				}
			}

		}
		return logger;
	}

	/**
	 * Log a message with info log level.
	 */
	public void info(Object message) {
		log(Level.INFO, String.valueOf(message), null);
	}

	/**
	 * Log a message and exception with info log level.
	 */
	public void info(Object message, Throwable exception) {
		log(Level.INFO, String.valueOf(message), exception);
	}

	/**
	 * Is debug logging currently enabled?
	 */
	public boolean isDebugEnabled() {
		return (logger.isLoggable(Level.FINE));
	}

	/**
	 * Is error logging currently enabled?
	 */
	public boolean isErrorEnabled() {
		return (logger.isLoggable(Level.SEVERE));
	}

	/**
	 * Is fatal logging currently enabled?
	 */
	public boolean isFatalEnabled() {
		return (logger.isLoggable(Level.SEVERE));
	}

	/**
	 * Is info logging currently enabled?
	 */
	public boolean isInfoEnabled() {
		return (logger.isLoggable(Level.INFO));
	}

	/**
	 * Is trace logging currently enabled?
	 */
	public boolean isTraceEnabled() {
		return (logger.isLoggable(Level.FINEST));
	}

	/**
	 * Is warn logging currently enabled?
	 */
	public boolean isWarnEnabled() {
		return (logger.isLoggable(Level.WARNING));
	}

	/**
	 * Log a message with trace log level.
	 */
	public void trace(Object message) {
		log(Level.FINEST, String.valueOf(message), null);
	}

	/**
	 * Log a message and exception with trace log level.
	 */
	public void trace(Object message, Throwable exception) {
		log(Level.FINEST, String.valueOf(message), exception);
	}

	/**
	 * Log a message with warn log level.
	 */
	public void warn(Object message) {
		log(Level.WARNING, String.valueOf(message), null);
	}

	/**
	 * Log a message and exception with warn log level.
	 */
	public void warn(Object message, Throwable exception) {
		log(Level.WARNING, String.valueOf(message), exception);
	}

	public static void setSendLog(boolean sendLog) {
		Jdk14Logger.sendLog = sendLog;
	}

}
